package com.kalvin.kvf.modules.dep.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.modules.dep.entity.Adv;
import com.kalvin.kvf.modules.dep.service.AdvService;
import com.kalvin.kvf.modules.sys.entity.Approve;
import com.kalvin.kvf.modules.sys.service.ApproveService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


/**
 * <p>
 *  前端控制器
 * </p>
 * @since 2021-04-21 21:17:26
 */
@RestController
@RequestMapping("dep/adv")
public class AdvController extends BaseController {

    @Autowired
    private AdvService advService;

    @Autowired
    private ApproveService approveService;

    @RequiresPermissions("dep:adv:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("dep/adv");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("dep/adv_edit");
        Adv adv;
        if (id == null) {
            adv = new Adv();
        } else {
            adv = advService.getById(id);
        }
        mv.addObject("editInfo", adv);
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(Adv adv) {
        Page<Adv> page = advService.listAdvPage(adv);
        return R.ok(page);
    }

    @RequiresPermissions("dep:adv:add")
    @PostMapping(value = "add")
    public R add(Adv adv) {
        adv.setStatus("待审批");
        advService.save(adv);
        Approve approve = new Approve();
        approve.setContent(adv.getContent());
        approve.setCreatime(adv.getCreatime());
        approve.setName(adv.getName());
        approve.setStatus(adv.getStatus());
        approveService.save(approve);
        return R.ok();
    }

    @RequiresPermissions("dep:adv:edit")
    @PostMapping(value = "edit")
    public R edit(Adv adv) {
        advService.updateById(adv);
        return R.ok();
    }

    @RequiresPermissions("dep:adv:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        advService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(advService.getById(id));
    }

}

